package Objetos;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.i18n.SimpleDateFormat;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import Bd.Bd;
import Main.Utils;

/**
 *
 * @author ARMGARCES
 */
public class Sesion implements KvmSerializable{
    
	private int id;
    private String horaIni;
    private String horaFin;
    private int iduser;
    private String user;
    private String pswd;
    private int codtel;
    /*
     * Para Notificar que el proceso se cumplio correctamente en el servidor
     */
    private int status;
    /**
     * 
     */
    public Sesion(){
    	
    }
    
    public Sesion(boolean b){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm");
    	String timenow = sdf.format(new Date(System.currentTimeMillis()));
    	setHoraInicio(timenow);
    	setHoraFin(null);
    	setCodtel(Bd.getCodigoTelf());
    	setStatus(0);    	
    }
    /**
     * 
     * @param _soap
     */
    public Sesion(final SoapObject _soap) {
		
    	int _stat = Integer.parseInt(_soap.getProperty("status").toString());
		setStatus(_stat);
    	
		int _id = Integer.parseInt(_soap.getProperty("id").toString());
		setId(_id);
		String _inicio = _soap.getProperty("horaIni").toString();
		setHoraInicio(_inicio);
		
		try{
			String _fin = _soap.getProperty("horaFin").toString();
			setHoraFin(_fin);
		}catch(Exception e){			
		}
		
		int _iduser = Integer.parseInt(_soap.getProperty("idUser").toString());
		setIdUser(_iduser);
		String _user = _soap.getProperty("user").toString();
		setUser(_user);
		
		int _codtel = Integer.parseInt(_soap.getProperty("codtel").toString());
		setCodtel(_codtel);
		
	}
    
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the hora
     */
    public String getHoraInicio() {
        return horaIni;
    }

    /**
     * @param hora the hora to set
     */
    public void setHoraInicio(String hora) {
        this.horaIni = hora;
    }
    
    /**
     * @param hora the hora to set
     */
    public void setHoraFin(String hora) {
        this.horaFin = hora;
    }

    /**
     * @return the hora
     */
    public String getHoraFin() {
        return horaFin;
    }
    
    /**
     * @return the iduser
     */
    public int getIdUser() {
        return iduser;
    }

    /**
     * @param iduser the iduser to set
     */
    public void setIdUser(int iduser) {
        this.iduser = iduser;
    }
    
    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the pswd
     */
    public String getPswd() {
        return pswd;
    }

    /**
     * @param pswd the pswd to set
     */
    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    /**
     * @return the codtel
     */
    public int getCodtel() {
        return codtel;
    }

    /**
     * @param codtel the codtel to set
     */
    public void setCodtel(int codtel) {
        this.codtel = codtel;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }
    
    /**
     * 
     * @return
     */
    public boolean isToday(){
    	
    	String st[] = Utils.splitString(this.getHoraInicio(),' ',false);
    	String fecha[] = Utils.splitString(st[0],' ',false);
    	int y = Integer.parseInt(fecha[0]);
    	int m = Integer.parseInt(fecha[1]);
    	int d = Integer.parseInt(fecha[2]);
    	
    	Date hoy = new Date();
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(hoy);
    	return false;
    }
    
    /**
     * @param index 
     * @return
     */
	public Object getProperty(int index) {
		switch (index) {
			case 0:
				return ""+getId();
			case 1:
				return getHoraInicio();
			case 2:
				return getHoraFin();
			case 3:
				return ""+getIdUser();
			case 4:
				return getUser();
			case 5:
				return getPswd();
			case 6:
				return ""+getCodtel();
		}
		return null;
	}
	
	/**
	 * @return 
	 */
	public int getPropertyCount() {
		return 7;
	}
	
	/**
	 * @param index
	 * @param properties
	 * @param info
	 */
	public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
		switch (index) {
	       case 0:
	           info.type = PropertyInfo.INTEGER_CLASS;
	           info.name = "id";
	           break;
	       case 1:
	           info.type = PropertyInfo.STRING_CLASS;
	           info.name = "horaIni";
	           break;
	       case 2:
	           info.type = PropertyInfo.STRING_CLASS;
	           info.name = "horaFin";
	           break;
	       case 3:
	           info.type = PropertyInfo.INTEGER_CLASS;
	           info.name = "iduser";
	           break;
	       case 4:
	           info.type = PropertyInfo.STRING_CLASS;
	           info.name = "user";
	           break;
	       case 5:
	           info.type = PropertyInfo.STRING_CLASS;
	           info.name = "pswd";
	           break;
	       case 6:
	           info.type = PropertyInfo.INTEGER_CLASS;
	           info.name = "codtel";
	           break;
		}
	}
	/**
	 * @param index
	 * @param value
	 */
	public void setProperty(int index, Object value) {
		// TODO Auto-generated method stub
		switch(index) {
		    case 0:
		        setId(Integer.parseInt(value.toString()));
		        break;
		    case 1:
		        setHoraInicio(value.toString());
		        break;
		    case 2:
		        setHoraFin(value.toString());
		        break;
		    case 3:
		    	setIdUser(Integer.parseInt(value.toString()));
		        break;
		    case 4:
		        setUser(value.toString());
		        break;
		    case 5:
		        setPswd(value.toString());
		        break;
		    case 6:
		    	setCodtel(Integer.parseInt(value.toString()));
		        break;
		    default:
		        break;
		}
	}
    
}
